package com.myk.game.gobangchess.ai.shock;

import java.util.ArrayList;

public class If_go_push {

    public static int[][] if_go_push(int[][] board) {
        int[][] convKernel_1 = {
                {1, 1},
                {1, 1}
        };

        int ifGoPushJudge = 0;
        ArrayList<int[]> idx1 = new ArrayList<>();
        ArrayList<int[]> idx2 = new ArrayList<>();

        int[][] convResult = conv2(board, convKernel_1);
        for (int i = 0; i < convResult.length; i++) {
            for (int j = 0; j < convResult[0].length; j++) {
                if (convResult[i][j] == 3) {
                    ifGoPushJudge = 1;
                    idx1.add(new int[]{i+1, j+1});
                }
                if (convResult[i][j] == -3) {
                    ifGoPushJudge = 1;
                    idx2.add(new int[]{i+1, j+1});
                }
            }
        }

        int[][] idx = new int[idx1.size() + idx2.size()][2];
        for (int i = 0; i < idx1.size(); i++) {
            idx[i] = idx1.get(i);
        }
        for (int i = 0; i < idx2.size(); i++) {
            idx[idx1.size() + i] = idx2.get(i);
        }

        return idx;
    }

    public static int[][] conv2(int[][] board, int[][] kernel) {
        int numRows = board.length;
        int numCols = board[0].length;
        int kernelRows = kernel.length;
        int kernelCols = kernel[0].length;
        int[][] result = new int[numRows - kernelRows + 1][numCols - kernelCols + 1];

        for (int i = 0; i < result.length; i++) {
            for (int j = 0; j < result[0].length; j++) {
                int sum = 0;
                for (int m = 0; m < kernelRows; m++) {
                    for (int n = 0; n < kernelCols; n++) {
                        sum += board[i + m][j + n] * kernel[m][n];
                    }
                }
                result[i][j] = sum;
            }
        }

        return result;
    }
}
